package com.ysw.experience.限流处理;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName RedisUtilV1
 * @Description redis 限流工具类
 * @Author ysw
 * @Date 2022/5/11 20:08
 */
@Component
public class RedisUtilV1 {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    void SimpleLimit(String redisKey, Long time, TimeUnit timeUnit, int permitTimes) {
        BoundValueOperations<String, String> operations = stringRedisTemplate.boundValueOps(redisKey);
        Long increment = operations.increment();
        // 第一次处理,加入过期时间
        if (increment == 1) {
            operations.expire(time, timeUnit);
        } else if (increment > permitTimes) {

        }
    }
}